package com.clock.service.impl;
import cn.hutool.core.date.DateUtil;
import com.clock.common.constant.HttpStatus;
import com.clock.common.constant.StudyTaskKey;
import com.clock.common.domain.R;
import com.clock.common.exception.BizException;
import com.clock.domain.entity.DailyRecord;
import com.clock.mapper.DailyRecordMapper;
import com.clock.service.DailyRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class DailyRecordServiceImpl implements DailyRecordService {

    @Resource
    DailyRecordMapper dailyRecordMapper;


    @Override
    public R addNewStudyRecord(DailyRecord dailyRecord) {

        if (dailyRecord.getUserId() == null || dailyRecord.getStudyTime() == null || dailyRecord.getCreateTime() == null) {
            throw new BizException(HttpStatus.BAD_REQUEST, "参数异常");
        }
        System.out.println(dailyRecord);

        int num = dailyRecordMapper.insert(dailyRecord);
        if (num < 1) {
            throw new BizException(HttpStatus.ERROR, "记录失败");
        }
        return R.ok("记录成功");
    }

    @Override
    public R queryRecordsByUserId(Long userId, Integer pageNum,Integer pageSize) {

        if (userId == null || pageNum == null || pageSize == null) {
            throw new BizException(HttpStatus.BAD_REQUEST, "参数异常");
        }

        PageHelper.startPage(pageNum, pageSize);
        List<DailyRecord> dailyRecords = dailyRecordMapper.queryRecordsByLimit(userId);
        PageInfo pageInfo = new PageInfo(dailyRecords);

        if (dailyRecords.isEmpty()) {
            throw new BizException(HttpStatus.NO_CONTENT, "暂无记录");
        }
        return R.ok(pageInfo);
    }

}
